<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class admin_donation extends CI_Controller {
    private $limit = 20;
    function __construct() {
        parent::__construct();
        $this->load->model('model_donation', 'm_dona', TRUE);
        $this->load->model('model_news', 'm_news', TRUE);
    }
    
    function index() {
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        if($this->session->userdata('admin_user')->level != 1){
            redirect("admin_home","refresh");
        }
        else{
            $data = array(
                'web_title' =>  'Admin Donation',
                'main_view' =>  'mimin/donation/index_new',
                'list_data' =>  $this->m_dona->get_all()
            );
        }
        
        $this->load->view('mimin/template/template_table',$data);
    }
    function delete(){
        if(!$this->session->userdata('admin_user')){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You need to re-login"
            );
            echo json_encode($output);exit();
        }
        if($this->session->userdata('admin_user')->level != 1){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You're not allowed to run this function"
            );
            echo json_encode($output);exit();
        }
        
        $this->form_validation->set_rules('id','id donasi','required');
        if($this->form_validation->run() == FALSE){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "ID Donation is missed"
            );
            echo json_encode($output);exit();
        }
        else{
            //SET TIME ZONE
            date_default_timezone_set("Asia/Jakarta");
            $curdate = date("Y-m-d h:i:s");
            
            //cek database
            $count = count($data_donasi = $this->m_dona->get_donation_by_id($this->input->post('id')));
            if($count > 0){
                //get datat status
                $status_dona = 0;
                foreach($data_donasi as $r):
                    $status_dona = $r->deleted;
                endforeach;
                
                $this->db->trans_begin();
                
                if($status_dona != 1){
                    $update_dona = array(
                        'deleted'    =>  1
                    );
                    $status_dona = $this->m_dona->update_donation($update_dona,$this->input->post('id'));
                    if($status_dona){
                        if ($this->db->trans_status() === FALSE)
                        {
                            $this->db->trans_rollback();
                            $output = array(
                                'status'    =>  0,
                                'msg'       =>  "Data deletion is failed"
                            );
                            echo json_encode($output);exit();
                        }
                        else
                        {
                            $this->db->trans_commit();
                            $output = array(
                                'status'    =>  1,
                                'msg'       =>  "Succesfully Change data status to deleted"
                            );
                            echo json_encode($output);exit();
                        }
                        
                    }
                    else{
                        $this->db->trans_rollback();
                        $output = array(
                            'status'    =>  0,
                            'msg'       =>  "Delete donation data unsuccessful, ERROR : ".  mysql_error()
                        );
                        echo json_encode($output);exit();
                    }
                }
                else{
                    $status_dona = $this->m_dona->delete($this->input->post('id'));
                    if($status_dona){
                        if ($this->db->trans_status() === FALSE)
                        {
                            $this->db->trans_rollback();
                            $output = array(
                                'status'    =>  0,
                                'msg'       =>  "Delete donation data permanently unsuccessful"
                            );
                            echo json_encode($output);exit();
                        }
                        else
                        {
                            $this->db->trans_commit();
                            $output = array(
                                'status'    =>  1,
                                'msg'       =>  "success Delete donation data permanently"
                            );
                            echo json_encode($output);exit();
                        }
                        
                    }
                    else{
                        $this->db->trans_rollback();
                        $output = array(
                            'status'    =>  0,
                            'msg'       =>  "Delete donation data permanently unsuccessful, ERROR : ".  mysql_error()
                        );
                        echo json_encode($output);exit();
                    }
                }
            }
            else{
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "Cannot found data, you should refresh the page"
                );
                echo json_encode($output);exit();
            }
        }
    }
    
    function undelete(){
        if(!$this->session->userdata('admin_user')){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You need to re-login"
            );
            echo json_encode($output);exit();
        }
        if($this->session->userdata('admin_user')->level != 1){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You're not allowed to run this function"
            );
            echo json_encode($output);exit();
        }
        $this->form_validation->set_rules('id','id donation','required');
        if($this->form_validation->run() == FALSE){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "ID Donation is missed"
            );
            echo json_encode($output);exit();
        }
        else{
            extract($_POST);
            $count = count($data_donasi = $this->m_dona->get_donation_by_id($id));
            if($count > 0){
                $this->db->trans_begin();
                $update_data = array(
                    'deleted'    =>  0
                );
                $status_update = $this->m_dona->update_donation($update_data,$id);
                if($status_update){
                    if ($this->db->trans_status() === FALSE)
                    {
                        $this->db->trans_rollback();
                        $output = array(
                            'status'    =>  0,
                            'msg'       =>  "Data restoration is failed, ERROR : ".  mysql_error()
                        );
                        echo json_encode($output);exit();
                    }
                    else
                    {
                        $this->db->trans_commit();
                        $output = array(
                            'status'    =>  1,
                            'msg'       =>  "Succesfully restore data to deleted"
                        );
                        echo json_encode($output);exit();
                    }
                }
                else{
                    $this->db->trans_rollback();
                    $output = array(
                        'status'    =>  0,
                        'msg'       =>  "restoration data unsuccessful, ERROR : ".  mysql_error()
                    );
                    echo json_encode($output);exit();
                }
            }
            else{
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "Data is missed, you should refresh the page"
                );
                echo json_encode($output);exit();
            }
        }
    }
    
    function detail($id=""){
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        elseif($id=="" || $this->session->userdata('admin_user')->level != 1){
            redirect("admin_donation","refresh");
        }
        else{
            //SET TIME ZONE
            date_default_timezone_set("Asia/Jakarta");
            $curdate = date("Y-m-d h:i:s");
            
            $count = count($list_data = $this->m_dona->get_donation_by_id($id));
            if($count >  0){
                $list_confirmation = $this->m_dona->get_donation_confirmation_by_id($id);
                        $data = array(
                            "main_view"     =>  "mimin/donation/detail",
                            "web_title"     =>  "Detail Donation",
                            "list_data"     =>  $list_data,
                            "list_confirm"  =>  $list_confirmation,
                        );
                        $this->load->view("mimin/template/template_table",$data);
                    
            }
            else{
                redirect("admin_donation","refresh");
            }
        }
    }

    function process(){
        if(!$this->session->userdata('admin_user')){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You need to re-login"
            );
            echo json_encode($output);exit();
        }
        if($this->session->userdata('admin_user')->level != 1){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You're not allowed to run this function"
            );
            echo json_encode($output);exit();
        }
        $this->form_validation->set_rules('id','id donasi','required');
        if($this->form_validation->run() == FALSE){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "ID Donation is missed"
            );
            echo json_encode($output);exit();
        }
        else{
            //SET TIME ZONE
            date_default_timezone_set("Asia/Jakarta");
            $curdate = date("Y-m-d h:i:s");
            
            //cek database
            $count = count($data_donasi = $this->m_dona->get_donation_by_id($this->input->post('id')));
            if($count > 0){
                //cek status donasi
                
                //get member name and status
                $member_name = "";
                $status_dona = 0;
                foreach($data_donasi as $r):
                    $member_name = $r->name;
                    $status_dona = $r->status;
                endforeach;
                
                //jika data sudah pernah diproses
                if($status_dona != 1){
                    $output = array(
                        'status'    =>  0,
                        'msg'       =>  "Data cannot processed"
                    );
                    echo json_encode($output);exit();
                }
                
                
                $this->db->trans_begin();
                
                //create news
                $isi_content = "Terimakasih kami ucapkan kepada <b>".$member_name."</b> yang telah mendonasikan kepada IPPI";
                $isi_news = array(
                    'judul'             =>  "Donasi oleh ".$member_name,
                    'content'           =>  $isi_content,
                    'datecreated'       =>  $curdate,
                    'createdby'         =>  $this->session->userdata('admin_user')->id_admin,
                    'image'             =>  "content/other/giving.png",
                );
                $status = $this->m_news->save($isi_news);
                if($status){
                    $update_dona = array(
                        'status'    =>  2
                    );
                    $status_dona = $this->m_dona->update_donation($update_dona,$this->input->post('id'));
                    if($status_dona){
                        if ($this->db->trans_status() === FALSE)
                        {
                            $this->db->trans_rollback();
                            $output = array(
                                'status'    =>  0,
                                'msg'       =>  "Processed donation unsuccessful"
                            );
                            echo json_encode($output);exit();
                        }
                        else
                        {
                            $this->db->trans_commit();
                            $output = array(
                                'status'    =>  1,
                                'msg'       =>  "Succesfully Processed donation"
                            );
                            echo json_encode($output);exit();
                        }
                        
                    }
                    else{
                        $this->db->trans_rollback();
                        $output = array(
                            'status'    =>  0,
                            'msg'       =>  "Processed donation unsuccessful, ERROR : ".  mysql_error()
                        );
                        echo json_encode($output);exit();
                    }
                }
                else{
                    $this->db->trans_rollback();
                    $output = array(
                        'status'    =>  0,
                        'msg'       =>  "Fail to create news data"
                    );
                    echo json_encode($output);exit();
                }
            }
            else{
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "Cannot found data, you should refresh the page"
                );
                echo json_encode($output);exit();
            }
        }
    }
    
    function check_donation($code=""){
        if(empty($code) || $code != md5("cemungudh")){
            die("Error Page".md5("cemungudh"));
        }
        else{
            date_default_timezone_set("Asia/Jakarta");
            $curdate = date_create(date("Y-m-d"));
            //get all data has 
            $condition = "(`status` != '1' AND `status` !='2') AND (`deleted` != '1' OR `deleted` IS NULL) AND `datecreated` < NOW()";
            $list_data = $this->m_dona->get_data_by_condition($condition);
            if(count($list_data)>0){
//                print_r($list_data);
//                $list_id = " (";
                foreach ($list_data as $r) :
                    $time = strtotime($r->datecreated);
                    $my_format = date_create(date("Y-m-d", $time));
                    $diff = date_diff($my_format,$curdate);
                    $selisih = $diff->format("%a");
                    if($selisih > 2){
                        $isi = array('deleted'  =>1);
                        $this->m_dona->update_donation($isi,$r->id_donasi);
                    }
                endforeach;
//                $length = strlen($list_id);
//                $list_id[$length-1] = "";
//                $list_id .=");";
//                print_r($list_id);
//                $status = $this->m_dona->update_by_condition($list_id);
//                var_dump($status);
            }
        }
    }
}

/* End of file admin_event.php */
/* Location: ./application/controllers/admin_event.php */